<template>
<!-- 用户列表 -->
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/userlist' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card class="user-list">
      <!-- 搜索框 -->
      <div class="userSearch">
        <el-input
          placeholder="请输入内容"
          clearable
          v-model="pageInfo.query"
          class="inputSearch"
          @input="searchUsers"
        >
          <el-button @click="searchUsers" slot="append" icon="el-icon-search"></el-button>
        </el-input>
        <el-button type="primary" class="addUserBtn" @click="addUserDialog">添加用户</el-button>
      </div>

      <!-- 数据列表 -->
      <el-table :data="userlist" border style="width: 100%;margin-top:20px">
        <el-table-column type="index" align="center" label="#" width="80"></el-table-column>
        <el-table-column align="center" prop="username" label="姓名" width="180"></el-table-column>
        <el-table-column align="center" prop="email" label="邮箱" width="180"></el-table-column>
        <el-table-column align="center" prop="mobile" label="电话"></el-table-column>
        <el-table-column align="center" label="创建时间">
          <template slot-scope="scope">
            <span>{{ scope.row.create_time | dateTime }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="role_name" label="角色"></el-table-column>
        <el-table-column align="center" label="状态">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.mg_state"
              active-color="#13ce66"
              inactive-color="#ff4949"
              @change="setUserState(scope.row)"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作" width="150">
          <template slot-scope="scope">
            <el-row>
              <!-- 编辑用户信息 -->
              <el-button
                size="mini"
                type="primary"
                icon="el-icon-edit"
                circle
                @click="handleEdit(scope.row)"
              ></el-button>
              <!-- 删除用户信息 -->
              <el-button
                size="mini"
                type="danger"
                icon="el-icon-delete"
                circle
                @click="handleDelete(scope.row)"
              ></el-button>
              <!-- 设置用户信息 -->
              <el-button
                size="mini"
                type="success"
                icon="el-icon-setting"
                circle
                @click="handleRole(scope.row)"
              ></el-button>
            </el-row>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页器 -->
      <el-pagination
        background
        layout="prev, pager, next, sizes, total, jumper"
        :current-page="pageInfo.pagenum"
        :page-sizes="[2, 4, 6]"
        :page-size="pageInfo.pagesize"
        :total="total"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
      ></el-pagination>

      <!-- 添加用户弹出框 -->
      <el-dialog title="添加用户" :visible.sync="isUserDialog">
        <el-form ref="addUserForm" :model="userinfo" :rules="rules">
          <el-form-item label="用户名" label-width="100px" prop="username">
            <el-input v-model="userinfo.username" autocomplete="off"></el-input>
          </el-form-item>

          <el-form-item label="密码" label-width="100px" prop="password">
            <el-input v-model="userinfo.password" autocomplete="off"></el-input>
          </el-form-item>

          <el-form-item label="邮箱" label-width="100px" prop="email">
            <el-input v-model="userinfo.email" autocomplete="off"></el-input>
          </el-form-item>

          <el-form-item label="手机" label-width="100px" prop="mobile">
            <el-input v-model="userinfo.mobile" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="isUserDialog = false">取 消</el-button>
          <el-button type="primary" @click.prevent="addUserConfirm">确 定</el-button>
        </div>
      </el-dialog>

      <!-- 编辑用户对话框 -->
      <el-dialog title="编辑用户" :visible.sync="isUserEdit">
        <el-form ref="editUserForm" :model="userinfo" :rules="rules">
          <el-form-item label="用户名" label-width="100px" prop="username">
            <el-input disabled v-model="userinfo.username" autocomplete="off"></el-input>
          </el-form-item>

          <el-form-item label="邮箱" label-width="100px" prop="email">
            <el-input v-model="userinfo.email" autocomplete="off"></el-input>
          </el-form-item>

          <el-form-item label="手机" label-width="100px" prop="mobile">
            <el-input v-model="userinfo.mobile" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="isUserEdit = false">取 消</el-button>
          <el-button type="primary" @click.prevent="editUserConfirm">确 定</el-button>
        </div>
      </el-dialog>

      <!-- 打开分配角色对话框 -->
      <el-dialog title="分配角色" :visible.sync="dialogFormRole">
        <el-form :model="roleForm">
          <div class="currentUsernfo">
            <p>当前用户：{{ currentUser.username }}</p>
            <p>当前角色：{{ currentUser.role_name }}</p>
            <p>{{ roleForm.id }}</p>
          </div>
          <el-form-item label="分配角色" label-width="200">
            <el-select v-model="roleForm.id" placeholder="请选择角色">
              <el-option
                v-for="(item, index) in roles"
                :label="item.roleName"
                :value="item.id"
                :key="index"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormRole = false">取 消</el-button>
          <el-button type="primary" @click="updateRole">确 定</el-button>
        </div>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import {
  userList,
  addUser,
  deleteUser,
  editUserInfo,
  updateUserStatus,
  getUserRoleList,
  updateUserRole,
  getUserRoleInfo,
} from "@/http/api";
import _ from "lodash";

export default {
  data() {
    //验证邮箱的函数
    const isEmail = (rule, value, callback) => {
      //邮箱正则
      // eslint-disable-next-line no-useless-escape
      let reg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
      let isTrue = reg.test(value);
      if (isTrue) {
        return callback();
      }

      callback(new Error("不符合邮箱的规则，请重新输入"));
    };

    const isMobile = (rule, value, callback) => {
      let reg = /^[1][3,4,5,7,6,9,8][0-9]{9}$/;
      let isTrue = reg.test(value);
      if (isTrue) {
        return callback();
      }

      callback(new Error("不符合手机的规则，请重新输入"));
    };

    //验证手机号的函数

    return {
      //添加用户表单验证规则
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            min: 6,
            max: 15,
            message: "长度在 6 到 15 个字符",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 6,
            max: 15,
            message: "长度在 6 到 15 个字符",
            trigger: "blur",
          },
        ],
        email: [
          { required: true, message: "请输入邮箱", trigger: "blur" },
          { validator: isEmail, trigger: "blur" },
        ],
        mobile: [
          { required: true, message: "请输入手机号", trigger: "blur" },
          { validator: isMobile, trigger: "blur" },
        ],
      },
      isUserDialog: false,
      userinfo: {
        username: "",
        password: "",
        email: "",
        mobile: "",
      },
      // query:查询参数 pagenum:当前页码 pagesize:每页显示条数
      pageInfo: {
        query: "",
        pagenum: 1,
        pagesize: 6,
      },
      total: 10,
      userlist: [],
      isUserEdit: false, //是否显示用户编辑对话框
      //是否显示分配角色对话框
      dialogFormRole: false,
      //用户角色列表
      roles: [],
      //当前用户信息
      currentUser: {},
      roleForm: {
        id: "",
      },
      currentRoleId: "",
    };
  },
  created() {
    //初始加载用户列表
    this.getUserList();
  },
  methods: {
    //显示用户对话框
    addUserDialog() {
      //进入时清空userinfo
      this.userinfo = {};
      //显示添加用户弹窗
      this.isUserDialog = true;
    },
    //确认添加用户
    addUserConfirm() {
      //调用封装的接口将数据添加到后台
      this.$refs.addUserForm.validate(async (valid) => {
        if (valid) {
          await addUser(this.userinfo);
          this.getUserList();
          this.userinfo = {
            username: "",
            password: "",
            email: "",
            mobile: "",
          };
          this.isUserDialog = false;
        } else {
          return false;
        }
      });
    },
    //搜索用户
    searchUsers: _.debounce(function () {
      this.getUserList();
    }, 300),
    //分页器
    handleCurrentChange(cpage) {
      //console.log(`当前页: ${cpage}`);
      this.pageInfo.pagenum = cpage;
      this.getUserList();
    },
    handleSizeChange(psize) {
      //console.log(`每页 ${psize} 条`);
      this.pageInfo.pagesize = psize;
      this.getUserList();
    },
    //获取用户列表
    async getUserList() {
      const res = await userList(this.pageInfo);
      const { pagenum, total, users } = res.result;

      this.pageInfo.pagenum = pagenum;
      this.userlist = users;
      this.total = total;
    },
    //删除用户信息//打开删除提示框
    handleDelete(row) {
      //   console.log(index, row);
      this.$confirm("此操作将永久删除该用户所有信息, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          deleteUser(row.id);
          this.getUserList();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //修改用户信息
    handleEdit(row) {
      //用lodash中的cloneDeep将user处理成深拷贝，不影响
      this.userinfo = _.cloneDeep(row);
      this.isUserEdit = true;
    },
    //确认修改信息提交接口
    editUserConfirm() {
      //利用refs
      this.$refs.editUserForm.validate(async (valid) => {
        if (valid) {
          // console.log(this.userinfo)
          // 获取参数--id
          const userid = this.userinfo.id;
          //向后台提交要编辑的数据
          await editUserInfo(userid, this.userinfo);
          //重新拉取数据
          this.getUserList();
          //隐藏框框
          this.isUserEdit = false;
        }
      });
    },
    //更新用户状态
    setUserState(row) {
      //解构拿值
      const { id, mg_state } = row;
      //传参至接口
      updateUserStatus(id, mg_state);
      //重新拉取数据
      this.getUserList();
    },
    //打开分配用户角色对话框
    async handleRole(user) {
      console.log("urer:", user);
      this.currentUser = user;
      this.dialogFormRole = true;
      const res = await getUserRoleList();

      console.log("用户角色列表：", res);
      this.roles = res.result;
    },
    //确认更新->向后台更新角色
    async updateRole() {
      /**
       * 第一步，先得到角色id
       * 第二步,通过角色id查询角色名和角色描述
       * 第三步：再调取修改角色接口，再更新当前用户角色
       */

      this.currentRoleId = this.roleForm.id;
      const res = await getUserRoleInfo(this.currentRoleId);
      // console.log('当前roleid对应的角色：',res)
      const { roleName, roleDesc } = res.result;
      // const result = await updateUserRole(this.currentRoleId, {
      //   roleName,
      //   roleDesc
      // });

      const result = await updateUserRole(this.currentUser.id, {
        rid: this.currentRoleId,
      });
      console.log("确认修改：", result);
      this.getUserList();

      this.dialogFormRole = false;
    },
  },
};
</script>

<style lang="scss" scoped>
.user-list {
  margin-top: 20px;
}

.inputSearch {
  width: 300px;
}

.addUserBtn {
  margin-left: 10px;
}
</style>